package com.duobao.view;
import java.awt.*;

import javax.swing.*;

import com.duobao.game.DuobaoSwingLaunch;
import com.duobao.server.*;
import com.duobao.uitl.*;

import java.awt.event.*;

public class Jianqi extends JPanel implements MouseListener{
    DuobaoSwingLaunch game;
    Map heroMap;
    Equip equip;
    JLabel fanhui;
    JLabel fanhuiyouxi;

    JLabel msName;
    JLabel describe[];
    JLabel tishi;
    JLabel suishen;
    public Jianqi(DuobaoSwingLaunch game, Equip equip)
    {
        this.game=game;
        this.heroMap=Maps.getMap(this.game.hero.getLocation());
        GridLayout gl=new GridLayout(16,1,1,1);
        this.setLayout(gl);
        this.equip = equip;

        setMsName();
        setDescribe();
        setTishi();
        setSuishen();
        setXiaoguo();



        setFanhui();
        setFanhuiyouxi();
    }
    private void setXiaoguo()
    {
        if(!this.equip.ifHuoBi())
        {
            if(this.equip.getCategory()==14 || this.equip.getCategory()==15)
            {
                this.game.hero.getEquip2().add(equip);
                equip.setVe(this.game.hero.getEquip2());
            }else
            {
                this.game.hero.getEquip2().add(equip);
            }

            equip.setZB(false);
        }else
        {
            this.game.hero.setHuobi(equip);
            equip.setZB(false);
        }
        this.heroMap.getEquips().remove(equip);
        this.game.hero.setFZ();
    }
    public void setMsName() {
        msName=new JLabel(this.equip.getMsName());
        msName.setFont(myFont.f2);
        msName.addMouseListener(this);
        JPanel jp1=new JPanel();
        jp1.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp1.add(msName);
        this.add(jp1);
    }
    public void setDescribe() {
        String Des[]=SplitString.splitString(this.equip.getDescribe());
        int leng=Des.length;
        describe=new JLabel[leng];

        for(int i=0;i<leng;i++)
        {
            describe[i]=new JLabel();
            describe[i].addMouseListener(this);
            describe[i].setFont(myFont.f2);
            describe[i].setText(Des[i]);
            JPanel jp1=new JPanel();
            jp1.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
            jp1.add(describe[i]);
            this.add(jp1);
        }
    }
    private void setTishi() {
        tishi=new JLabel("你把"+equip.getName()+"捡了起来。");
        tishi.setFont(myFont.f2);
        tishi.addMouseListener(this);
        JPanel jp1=new JPanel();
        jp1.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp1.add(tishi);
        this.add(jp1);
    }
    public void setSuishen() {
        suishen=new JLabel();
        suishen.setFont(myFont.f2);
        suishen.addMouseListener(this);
        suishen.setForeground(Color.BLUE);
        suishen.setText("<html><u>"+"随身物品"+"</u></html>");
        JPanel jp1=new JPanel();
        jp1.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp1.add(suishen);
        this.add(jp1);
    }
    private void setFanhui() {
        fanhui=new JLabel();
        fanhui.addMouseListener(this);
        fanhui.setFont(myFont.f2);
        fanhui.setForeground(Color.BLUE);
        fanhui.setText("<html><u>"+"返回"+"</u></html>");
        JPanel jp3=new JPanel();
        jp3.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp3.add(fanhui);
        this.add(jp3);
    }

    private void setFanhuiyouxi() {
        fanhuiyouxi=new JLabel();
        fanhuiyouxi.addMouseListener(this);
        fanhuiyouxi.setFont(myFont.f2);
        fanhuiyouxi.setForeground(Color.BLUE);
        fanhuiyouxi.setText("<html><u>"+"返回游戏"+"</u></html>");
        JPanel jp4=new JPanel();
        jp4.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp4.add(fanhuiyouxi);
        this.add(jp4);
    }
    private boolean iFText(JLabel jl)
    {
        boolean f=false;
        if(jl==tishi)
        {
            f=true;
        }
        return f;
    }

    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource()==suishen)
        {
            try {
                this.game.setPanel(new Shuishen(this.game,Dishang.class.getDeclaredConstructor(DuobaoSwingLaunch.class,int.class),1));
            } catch (NoSuchMethodException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (SecurityException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }else if(e.getSource()==fanhuiyouxi)
        {
            this.game.setPanel(new MainPanel(this.game,1));
        }else if(e.getSource()==fanhui)
        {
            try {
                this.game.setPanel(new Dishang(this.game,1));
            }catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    }
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub
        if(iFText((JLabel)e.getSource()))
        {
            ((JLabel)e.getSource()).setCursor(new Cursor(Cursor.TEXT_CURSOR));
        }
    }
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

    }
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }
}
